#include <iostream>
#include <vector>
#include <string>
#include <pthread.h>
#include <unistd.h>

#include "blockQueue.hpp"
#include "Thread.hpp"

using namespace std;
using namespace Thread;

void consumer(blockQueue<int>& bq)
{
    cout << "consumer" << endl;

    int data;
    while(true)
    {
        bq.popQueue(data);
        cout << "consumer popQueue : " << data << endl;
    }
}

void productor(blockQueue<int>& bq)
{
    cout << "productor" << endl;

    int cnt = 1;
    while(true)
    {
        bq.enQueue(cnt);
        cout << "productor enQueue : " << cnt++ << endl;
        sleep(1);
    }
}

void startComment(vector<myThread<blockQueue<int>>>& threads, int num, blockQueue<int>& bq, func_t<blockQueue<int>> func)
{
    for (int i = 0; i < num; i++)
    {
        string name = "thread - " + to_string(i + 1);
        threads.emplace_back(func, bq, name);
        threads.back().start();
    }
}

void startConsumer(vector<myThread<blockQueue<int>>>& threads, int num, blockQueue<int>& bq)
{
    startComment(threads, num, bq, consumer);
}

void startProductor(vector<myThread<blockQueue<int>>>& threads, int num, blockQueue<int>& bq)
{
    startComment(threads, num, bq, productor);
}

void waitAll(vector<myThread<blockQueue<int>>>& threads)
{
    for (auto& thread : threads)
    {
        thread.join();
    }
}

int main()
{
    blockQueue<int> bq(5);

    vector<myThread<blockQueue<int>>> threads;

    startConsumer(threads, 1, bq);

    //sleep(1);

    startProductor(threads, 1, bq); 

    waitAll(threads);


    return 0;
}